<template>
  <el-dialog
    title="添加记录"
    :visible.sync="dialogVisible"
    width="600px"
    customClass="mes_dialog"
    v-dialogDrag
    :close-on-click-modal="false">
    <div class="raw_data_cont" :class="{more_list: rawDataList.length > 10}">
      <el-scrollbar>
        <div class="raw_item"
             :key="item.label"
             v-for="item in rawDataList">
          <div class="raw_label" v-html="item.label"></div>
          <div class="raw_input">
            <el-input v-model="item.val" maxLength="255" size="small" placeholder="扫码或输入字段，用@隔开"></el-input>
          </div>
        </div>
      </el-scrollbar>
    </div>
    <div class="raw_add_item">
      <span class="add_item_btn" @click="addItem"><i class="iconfont icon-btn_addnew"></i>添加10条</span>
    </div>
    <div slot="footer" class="mes_btn">
      <el-button type="primary" @click="formatData">提交</el-button>
      <el-button @click="_close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'AddData',
  data() {
    return {
      dialogVisible: false,
      templateId: '', // 模板id
      listNum: 0, // @的个数
      rawDataList: [], // 录入记录列表
    }
  },
  methods: {
    // 添加10条空字段
    addItem(num) {
      let sum = typeof num === 'number' ? num : 10
      for (let i = 0; i < sum; i++) {
        this.rawDataList.push({label: `${this.rawDataList.length + 1}`, val: ''})
      }
    },
    // 显示弹框
    openDialog(templateId, listNum) {
      this.templateId = templateId
      this.listNum = listNum + 1
      this.init()
      this.dialogVisible = true
    },
    // 初始化
    init() {
      this.rawDataList = []
      this.addItem(10)
    },
    // 格式化数据
    formatData() {
      let postData = this.rawDataList.filter(item => item.val).reduce((pre, cur) => [...pre, cur.val], [])
      if (!postData.length) {
        return this.$message({
          type: 'error',
          message: '请至少输入一条记录',
          duration: 1500,
          center: true
        })
      }
      for (let i = 0; i < postData.length; i++) {
        let arr = postData[i].split('@')
        if (arr.length !== this.listNum) {
          return this.$message({
            type: 'error',
            message: '记录的格式不对',
            duration: 1500,
            center: true
          })
        } else if (!arr[0]) {
          return this.$message({
            type: 'error',
            message: '采购批次号必填',
            duration: 1500,
            center: true
          })
        } else if (arr[0].length > 32) {
          return this.$message({
            type: 'error',
            message: '采购批次号长度不能大于32',
            duration: 1500,
            center: true
          })
        }
      }
      this.submit(postData.join(','))
    },
    // 保存
    submit(postData) {
      this.$store.dispatch('saveData', {
        method: 'post',
        url: 'a/rawmaterial/rawMaterial/saveRawMaterial',
        data: {
          templateId: this.templateId,
          values: postData
        },
        noCommit: true
      }).then(() => {
        this.dialogVisible = false
        this.$emit('saveSuccess')
        this.$message({
          type: 'success',
          message: '保存成功',
          duration: 1500,
          center: true
        })
      })
    },
    // 关闭弹框
    _close() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped lang="less">
  .raw_data_cont{
    border: 1px solid #dbe1e8;
    height: 409px;
    &.more_list{
      height: 430px;
    }
    .el-scrollbar {
      height: 100%;
    }
  }
</style>
